﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Molia.Premiere
{
    /// <summary>
    /// Login.xaml 的交互逻辑
    /// </summary>
    public partial class Login : Window
    {
        ViewModal.LoginViewModal vm;
        public Login()
        {
            InitializeComponent();
            vm = this.DataContext as ViewModal.LoginViewModal;
        }

        private void _Text_Change(object sender,TextChangedEventArgs e)
        {
            if(sender is TextBox)
            {
                var _t = sender as TextBox;
                if (!string.IsNullOrEmpty(_t.Text) && Warning.Visibility == Visibility.Visible)
                {
                    Warning.Visibility = Visibility.Hidden;
                }
            }
        }
        private void _Passwd_Change(object sender,RoutedEventArgs e)
        {
            if (sender is PasswordBox)
            {
                var _t = sender as PasswordBox;
                if (!string.IsNullOrEmpty(_t.Password) && Warning.Visibility == Visibility.Visible)
                {
                    Warning.Visibility = Visibility.Hidden;
                }
            }
        }
        private void _Login(object sender,RoutedEventArgs e)
        {
            if (!string.IsNullOrEmpty(vm.Text_ID) && (!string.IsNullOrEmpty(pw.Password)))
            {
                LoginPanel.Visibility = Visibility.Hidden;
                WaitPanel.Visibility = Visibility.Visible;
                Storyboard s = (Storyboard) FindResource("OnUnload");
                s.RepeatBehavior = new RepeatBehavior(new TimeSpan(0, 1, 0));
                s.Begin(WaitPanel);
                vm.OnFeedBack = (result) =>
                {
                    if (result)
                    {
                        s.Pause(WaitPanel);
                        this.Dispatcher.Invoke(() =>
                        {
                            MainWindow mw = new MainWindow();
                            mw.Show();
                            this.Close();
                        });
                    }
                    else
                    {
                        s.Pause(WaitPanel);
                        WaitPanel.Visibility = Visibility.Hidden;
                        LoginPanel.Visibility = Visibility.Visible;
                        Warning.Visibility = Visibility.Visible;
                    }
                };
                MessageBox.Show(pw.Password);
                vm.OnFeedBack(true);
            }
            else
            {
                Warning.Text = "密码或账号不允许为空";
                Warning.Visibility = Visibility.Visible;
            }
        }
    }
}
